package org.lq.er.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.lq.er.dao.Exam_informationDao;
import org.lq.er.entity.Exam_information;
import org.lq.er.util.Instantiation;
import org.lq.er.util.JDBCUtil;

import sun.security.timestamp.TSRequest;
/**
 * 
 * @author 韩伟伟
 *
 * @package org.lq.er.dao.impl
 */
public class Exam_informationDaoImpl implements Exam_informationDao<Exam_information> {

	@Override
	public int save(Exam_information t) {
		
		int num = 0;
		String sql = "insert into Exam_information(stu_id,subject_id,exam_position_id,status_check,status_pay,seat_num)values(?,?,?,?,?,?)";
		
		try {
		num = JDBCUtil.executeUpdate(sql, t.getStu_id(),t.getSubject_id(),t.getExam_position_id(),0,0,0);
			System.out.println(num);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int update(Exam_information t) {
		
		int num = 0;
		String sql = "update Exam_information set stu_id = ?,subject_id = ?,exam_position_id = ?,status_check = ?,status_pay=?,seat_num=? where id = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getStu_id(),t.getSubject_id(),t.getExam_position_id(),t.getStatus_check(),t.getStatus_pay(),t.getSeat_num(),t.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	@Override
	public int delete(int id) {
		
		int num = 0;
		try {
			num = JDBCUtil.executeUpdate("delete from Exam_information where id = ?",id);
			System.out.println(num);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public List<Exam_information> findAll() {
		
		List<Exam_information> list = null;
		
		try {
			list = JDBCUtil.executeQuery("select * from Exam_information", new Instantiation<Exam_information>() {

					@Override
					public Exam_information instance(ResultSet rs)  {
						Exam_information ef = new Exam_information();
						
							try {
								ef.setExam_position_id(rs.getInt("exam_position_id"));
								ef.setId(rs.getInt("id"));
								ef.setStu_id(rs.getString("stu_id"));
								ef.setSubject_id(rs.getInt("subject_id"));
								ef.setStatus_check(rs.getInt("status_check"));
								ef.setStatus_pay(rs.getInt("status_pay"));
								ef.setSeat_num(rs.getInt("seat_num"));
							} catch (SQLException e) {
								e.printStackTrace();
							}
						
						return ef;
					}

				});
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public Exam_information getById(int id) {
		List<Exam_information> list = null;
		try {
			list  = JDBCUtil.executeQuery("select * from Exam_information where id = ?",new Instantiation<Exam_information>() {
				Exam_information  ef = null;
				@Override
				public Exam_information instance(ResultSet rs) {
					ef = new Exam_information();
					try {
						ef.setExam_position_id(rs.getInt("exam_position_id"));
						ef.setId(rs.getInt("id"));
						ef.setStu_id(rs.getString("stu_id"));
						ef.setSubject_id(rs.getInt("subject_id"));
						ef.setStatus_check(rs.getInt("status_check"));
						ef.setStatus_pay(rs.getInt("status_pay"));
						ef.setSeat_num(rs.getInt("seat_num"));
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return ef;
				}
			},id);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list.get(0);
	}
	
	@Override
	public Exam_information getByExam_position_id(int epid) {
		List<Exam_information> list = null;
		try {
			list  = JDBCUtil.executeQuery("select * from Exam_information where exam_position_id = ?",new Instantiation<Exam_information>() {
				Exam_information  ef = null;
				@Override
				public Exam_information instance(ResultSet rs) {
					ef = new Exam_information();
					try {
						ef.setExam_position_id(rs.getInt("exam_position_id"));
						ef.setId(rs.getInt("id"));
						ef.setStu_id(rs.getString("stu_id"));
						ef.setSubject_id(rs.getInt("subject_id"));
						ef.setStatus_check(rs.getInt("status_check"));
						ef.setStatus_pay(rs.getInt("status_pay"));
						ef.setSeat_num(rs.getInt("seat_num"));
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return ef;
				}
			},epid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()>0) {
			return list.get(0);
		} else {
			return null;
		}
		
	}

	@Override
	public Exam_information getByStu_Id(String stuid) {
		
		List<Exam_information> list = null;
		try {
			list  = JDBCUtil.executeQuery("select * from Exam_information where stu_id = ?",new Instantiation<Exam_information>() {
				Exam_information  ef = null;
				@Override
				public Exam_information instance(ResultSet rs) {
					ef = new Exam_information();
					try {
						ef.setExam_position_id(rs.getInt("exam_position_id"));
						ef.setId(rs.getInt("id"));
						ef.setStu_id(rs.getString("stu_id"));
						ef.setSubject_id(rs.getInt("subject_id"));
						ef.setStatus_check(rs.getInt("status_check"));
						ef.setStatus_pay(rs.getInt("status_pay"));
						ef.setSeat_num(rs.getInt("seat_num"));
					} catch (SQLException e) {
						e.printStackTrace();
					}
					return ef;
				}
			},stuid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()>0) {
			return list.get(0);
		} else {
			return null;
		}
	}
}
